layui.define(['jquery','form'], function (exports) {
	"use strict";
	var $ = layui.jquery,form=layui.form,object = {
		c: {data:[]}, // 配置项
		f: function (c) { // 数据初始化
			var s=new Date().getTime(),t = this;
			t.c.id = c.id?c.id:'simple_table_TCW_RunningBack';
			t.c.tableId = c.tableelem?c.tableelem:'table-'+s;
			//c.data = JSON.parse('[{"accountName":"费玉清","timeTypeName":"00:00~00:59","operatorName":"周杰伦"}]');
			t.c.data = c.data; // 数据
			
			/**自定义的字段，是否流转中**/
			t.c.onProcess = c.onProcess;
		},
        load:function () { // 加载控件
        	var h = ``
        		+ `<div class="layui-form layui-form-item" lay-filter="` + this.id + `" style="background-color: #ffffff;">`
    				+ `<label style="font-weight: 700;font-size: 18px; padding: 5px;">流水列表</label>`
        		+ `</div>`
        		+ `<div class="layui-table-tool" style="padding: 0; min-height: 0; background-color: #ffffff;">`
        			+ `<div class="layui-table-tool-temp">`
        				+ `<div class="layui-btn-container">`
        					+ `<a class="layui-btn layui-btn-normal layui-btn-sm ` + (this.c.onProcess == 1 ? `layui-btn-disabled` : ``) + `" lay-event="` + (this.c.onProcess == 0 ? `event-to-select` : ``) + `"><i class="iconfont icon-add-btn"></i>选取流水</a>`
    					+ `</div>`
    				+ `</div>`
    			+ `</div>`
            	+ `<div class="layui-form layui-form-item" pane>`
            		+ `<div id="`+this.c.tableId+`">`
            			+ this.loadTable()
            		+ `</div>`
        		+ `</div>`
    		+ `</div>`;
            $('#'+this.c.id).html(h);
            this.listenBtn();
        },
        loadTable:function () { // 加载表格
            return ``
        	+ `<div class="layui-table-box">`
        		+ `<div class="layui-table-header" style="height: 300px; max-height: 300px; overflow: auto; background-color: #ffffff;">`
	            	+ `<table cellspacing="0" cellpadding="0" border="0" class="layui-table" lay-skin="grid" lay-even="" style="margin: 0;">`
	            		+ `<thead>`
	            			+ this.loadTableOfThead()
	            		+ `</thead>`
	            		+ `<tbody>` + this.loadTableOfTbody()+`</tbody>`
	        		+ `</table>`
        		+ `</div>`
    		+ `</div>`;
        },
        loadTableOfThead:function () { // 加载表头
            var h = `<thead>`;
            h +=`<tr>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">唯一识别码</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">划出运营公司</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">划入运营公司</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">交易类型</th>`;
            h += `<th style="width:4%; text-align: center; color: #000000;">金额</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">备注</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">唯一识别码</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">划出运营公司</th>`;
            h += `<th style="width:5%; text-align: center; color: #000000;">划入运营公司</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">交易类型</th>`;
            h += `<th style="width:4%; text-align: center; color: #000000;">金额</th>`;
            h += `<th style="width:8%; text-align: center; color: #000000;">备注</th>`;
            
            if(this.c.onProcess == 0) {
                h += `<th style="width:4%; text-align: center; color: #000000;">操作</th>`;
            }
            h += `</tr>`;
            h + `</thead>`;
			return h;
        },
        loadTableOfTbody:function () { // 加载表tbody
        	var t = this;
        	var h = `<tbody>`;
            for(var i=0;i<t.c.data.length;i++){
	            h += `<tr>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].runningIdOne + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vgsbmFromOne + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vgsbmToOne + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vjylxOne + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].fjeOne + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vbzOne + `</td>`;
	            h += `<td style="text-align:center; padding: 0;"><input type="text" class="layui-input" value="` + (t.c.data[i].runningIdOne ? t.c.data[i].runningIdTwo : '') + `" readonly="readonly"><i style="position: absolute; right: 2px; top:0px; height: 30px; line-height: 30px; width: 25px;" class="layui-icon layui-icon-more" data-type="runningIdTwo" data-index="` + i + `" lay-filter="buttontextModel"></i></td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vgsbmFromTwo + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vgsbmToTwo + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vjylxTwo + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].fjeTwo + `</td>`;
	            h += `<td style="text-align:center; padding: 0;">` + t.c.data[i].vbzTwo + `</td>`;
	            if(this.c.onProcess == 0) {
		            h += `<td style="text-align:center; padding: 0;"><button type="button" class="layui-btn layui-btn-xs layui-btn-danger" data-index="` + i + `" lay-filter="removeSingle"><i class="layui-icon layui-icon-delete"></i>移除</button></td>`;
	            }
                h+= `</tr>`;
            }
            
            h += `</tbody>`;
			return h;
        },
        listenBtn:function () { // 添加监听
            var t = this;
			
            // 监听“新增”按钮
            $('a[lay-event="event-to-select"]').on('click',function (e) {
            	
            	// 组装已选中的数据行
            	var selectedData = [];
                for(var i=0;i<t.c.data.length;i++){
                	var data = t.c.data[i];
                	selectedData.push({
                		id: data.runningIdOne,
                		vGsBm: data.vgsbmFromOne
                	});
                	selectedData.push({
                		id: data.runningIdTwo,
                		vGsBm: data.vgsbmToTwo
                	});
                }
            	
            	layui.YsModal.init({modalKey: 'TCW_AccountDetailedForCompanyForRunningBack', type: 'checkbox', uniqueKey: 'id', parentObj: t, selectedData: selectedData})
            });
            
            var domTable = $('#' + t.c.tableId);
			// 监听表格中各个输入框
            domTable.find('input[lay-filter="_input"]').on('change',function (e) {
				var index = $(this).attr('data-index');
				var key = $(this).attr('data-type');
				var value = $(this).val();
				t.c.data[index][key] = value;
				return;
			});
            
			// 监听弹出框表单
            domTable.find('i[lay-filter="buttontextModel"]').on('click',function (e) {
				var indexOfRow = $(this).attr('data-index');
            	
            	layui.YsModal.init({modalKey: 'TCW_AccountDetailedForCompanyForRunningBack', colCode: indexOfRow, parentObj: t})

        	});
            
			// 监听表格中移除按钮
            domTable.find('button[lay-filter="removeSingle"]').on('click',function (e) {
				var indexOfRow = $(this).attr('data-index');
				t.removeRow(indexOfRow);
				return;
			});
            
            // 表单控件重新加载
        	form.render();
        },
		matchValuesAfterModalSelectRadio:function (colCode, rowSelected, extraData) { // 选中数据后，匹配表单数值，用于YsModal回调
        	var t = this;

			t.updateRowAfterSelect(colCode, rowSelected);
		},
		matchValuesAfterModalSelectCheckbox:function (rowsSelected, extraData) { // 选中数据后，匹配表单数值，用于YsModal回调
        	var t = this;
        	
        	var ids = '';
        	for (var i = 0; i < rowsSelected.length; i++) {
				ids = ids + rowsSelected[i].id + ',';
			}

			var indexOfLoading = layer.load(1, {
				shade: [0.5,'#000000'] //0.1透明度的白色背景
			});
			
			$.ajax({
				type: "post",
				url: "/api/inter/tcwRunningBack/checkForAdd",
				data: {ids : ids.substring(0, ids.length - 1)},
				dataType: "json",
				success: function (res) {
					if(res.code != 0) {
						layer.msg(res.msg);
					} else {
						t.updateRows(res.data.rows);
					}
					layer.close(indexOfLoading);
				},
				error: function () {
				    alert("加载失败！")
				}
			});
		},
        updateRowAfterSelect:function (index, data) { // 更新数据行
        	var t = this;
        	var rowCurrent = t.c.data[index];
        	rowCurrent.runningIdTwo = data.id;
        	rowCurrent.vgsbmFromTwo = data.vGsBm;
        	rowCurrent.vgsjcFromTwo = data.vGsJc;
        	rowCurrent.vgsbmToTwo = data.vGsBmP;
        	rowCurrent.vgsjcToTwo = data.vGsJcP;
        	rowCurrent.vjylxTwo = data.vJyLx;
        	rowCurrent.fjeTwo = data.fJe;
        	rowCurrent.vbzTwo = data.vBz;
			
			return this.reload(t.c);
			
        },
        removeRow:function (index) { // 重新加载表格
        	var t = this;
        	t.c.data.splice(index, 1);
			return t.reload(t.c);
        },
        reloadTable:function () { // 重新加载表格
            $('#'+this.c.tableId).html(this.loadTable());
            this.listenBtn();
        },
        getData:function () {
            return this.c.data;
        },
        init:function (c) {this.f(c);this.load();return object;},
        reload:function (c) {c.data = this.c.data;$('#' + this.c.id).empty();return this.init(c);},
        addRow:function (row) {
        	var c = this.c;
        	c.data.push(row);
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        addRows:function (rows) {
        	var c = this.c;
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        updateRows:function (rows) {
        	var c = this.c;
        	c.data = [];
        	for (var i = 0; i < rows.length; i++) {
	        	c.data.push(rows[i]);
			}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRow:function (index, row) {
        	var c = this.c;
			c.data[index] = row;
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        editRowOfAttr:function (index, row) {
        	var c = this.c;
        	for(var key in row){
        		c.data[index][key] = row[key]
        	}
        	$('#' + c.id).empty();
        	return this.reload(c);
        },
        reloadRows:function (rows) {
        	var c = this.c;
        	c.data = rows;
        	return this.reload(c);
        }
	};
	exports('simple_table_TCW_RunningBack', object);
});